package com.ycdx.experts.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ycdx.experts.pojo.Expert;
import com.ycdx.experts.utils.DBPool;

public class ExpertImpl {
	
	/**
	 * 获取所有专家信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Expert> getAllExperts(){
		ArrayList<Expert> result = new ArrayList<Expert>();
		String sql = "select * from tb_expert order by id;";
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		
		try {
			result = (ArrayList<Expert>) qr.query(conn, sql, new BeanListHandler(Expert.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取所有专家信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Expert> searchExpertByField(String field){
		ArrayList<Expert> result = new ArrayList<Expert>();
		String sql = "select * from tb_expert where field like ?;";
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		Object[] params = {"%"+field+"%"};
		
		try {
			result = (ArrayList<Expert>) qr.query(conn, sql, new BeanListHandler(Expert.class),params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 根据ID获取专家信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Expert getExpertById(String id){
		Expert result = new Expert();
		String sql = "select * from tb_expert where id=?;";
		Object[] params = {id};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		
		try {
			result = (Expert) qr.query(conn, sql, new BeanHandler(Expert.class),params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
